<template>
  <div class="calendar-view">
    <div class="calendar-header">
      <el-button @click="prevMonth" circle>
        <el-icon><ArrowLeft /></el-icon>
      </el-button>
      <h3>{{ currentMonth }}</h3>
      <el-button @click="nextMonth" circle>
        <el-icon><ArrowRight /></el-icon>
      </el-button>
    </div>

    <div class="calendar-grid">
      <div class="weekday" v-for="day in weekdays" :key="day">
        {{ day }}
      </div>

      <div
          v-for="day in calendarDays"
          :key="day.date"
          class="calendar-day"
          :class="{
          'current-month': day.isCurrentMonth,
          'today': day.isToday,
          'checked': day.isChecked,
          'selected': day.date === selectedDate
        }"
          @click="selectDay(day)"
      >
        {{ day.day }}
        <el-icon v-if="day.isChecked" class="check-icon">
          <Check />
        </el-icon>
      </div>
    </div>
  </div>
</template>

<script setup lang="ts">
import { ref, computed, onMounted } from 'vue';
import { ArrowLeft, ArrowRight, Check } from '@element-plus/icons-vue';

const props = defineProps({
  checkedDates: {
    type: Array as () => string[],
    default: () => []
  }
});

const emit = defineEmits(['date-selected']);

// 当前显示的月份
const currentDate = ref(new Date());
// 当前选中的日期
const selectedDate = ref('');

// 周几显示
const weekdays = ['日', '一', '二', '三', '四', '五', '六'];

// 格式化日期为YYYY-MM-DD
const formatDateToLocal = (date: Date) => {
  const year = date.getFullYear();
  const month = String(date.getMonth() + 1).padStart(2, '0');
  const day = String(date.getDate()).padStart(2, '0');
  return `${year}-${month}-${day}`;
};

// 判断是否为今天
const isToday = (date: Date) => {
  const today = new Date();
  return date.getDate() === today.getDate() &&
      date.getMonth() === today.getMonth() &&
      date.getFullYear() === today.getFullYear();
};

// 生成日历天数
const calendarDays = computed(() => {
  const year = currentDate.value.getFullYear();
  const month = currentDate.value.getMonth();
  const today = new Date();

  // 当月第一天
  const firstDay = new Date(year, month, 1);
  // 当月最后一天
  const lastDay = new Date(year, month + 1, 0);
  // 日历开始日期 (上个月的最后几天)
  const startDay = new Date(firstDay);
  startDay.setDate(startDay.getDate() - firstDay.getDay());

  const days = [];
  const dayCount = 42; // 6周

  for (let i = 0; i < dayCount; i++) {
    const date = new Date(startDay);
    date.setDate(date.getDate() + i);
    const dateString = formatDateToLocal(date);

    days.push({
      date: dateString,
      day: date.getDate(),
      isCurrentMonth: date.getMonth() === month,
      isToday: isToday(date),
      isChecked: props.checkedDates.includes(dateString),
      isSelected: dateString === selectedDate.value
    });
  }

  return days;
});

// 当前月份显示文本
const currentMonth = computed(() => {
  const year = currentDate.value.getFullYear();
  const month = currentDate.value.getMonth() + 1;
  return `${year}年${month}月`;
});

// 上个月
const prevMonth = () => {
  const date = new Date(currentDate.value);
  date.setMonth(date.getMonth() - 1);
  currentDate.value = date;
};

// 下个月
const nextMonth = () => {
  const date = new Date(currentDate.value);
  date.setMonth(date.getMonth() + 1);
  currentDate.value = date;
};

// 选择日期
const selectDay = (day: any) => {
  if (day.isCurrentMonth) {
    // 更新选中的日期
    selectedDate.value = day.date;
    // 通知父组件
    emit('date-selected', day.date);
  }
};

// 组件挂载时设置默认选中当天
onMounted(() => {
  const today = formatDateToLocal(new Date());
  selectedDate.value = today;
  // 通知父组件默认选中当天
  emit('date-selected', today);
});

// 监听月份变化，如果选中的日期不在当前月份，自动选中当月第一天
const currentMonthYear = computed(() => {
  return {
    year: currentDate.value.getFullYear(),
    month: currentDate.value.getMonth()
  };
});
</script>

<style scoped>
.compact-calendar {
  padding: 8px;
  height: 240px;
}

.calendar-view{
  background: #fff;
  border-radius: 8px;
  box-shadow: 0 1px 6px rgba(0, 0, 0, 0.08);
  padding: 10px;
}

.calendar-header {
  display: flex;
  align-items: center;
  justify-content: space-between;
  margin-bottom: 8px;
}

.calendar-header h3 {
  margin: 0;
  font-size: 14px;
  font-weight: normal;
}

.calendar-grid {
  display: grid;
  grid-template-columns: repeat(7, 1fr);
  gap: 4px;
}

.weekday {
  text-align: center;
  font-size: 12px;
  color: #909399;
  padding: 2px 0;
}

.calendar-day {
  aspect-ratio: 1;
  display: flex;
  align-items: center;
  justify-content: center;
  border-radius: 50%;
  cursor: pointer;
  position: relative;
  font-size: 12px;
  height: 24px;
  margin: 0 auto;
  transition: all 0.2s ease;
}

.calendar-day.current-month {
  color: #333;
}

.calendar-day:not(.current-month) {
  color: #c0c4cc;
}

.calendar-day.today {
  background-color: var(--el-color-primary-light-9);
  color: var(--el-color-primary);
  font-weight: bold;
}

.calendar-day.checked {
  background-color: var(--el-color-primary-light-8);
  color: var(--el-color-primary);
}

.calendar-day.selected {
  background-color: var(--el-color-primary);
  color: white;
  font-weight: bold;
  box-shadow: 0 2px 4px rgba(64, 158, 255, 0.3);
}

.calendar-day.checked .check-icon {
  position: absolute;
  bottom: 1px;
  right: 1px;
  font-size: 10px;
  color: var(--el-color-primary);
}

.calendar-day.selected .check-icon {
  color: white;
}

.calendar-day:hover:not(.selected) {
  background-color: #f5f7fa;
}
</style>
